package com.chixing.service.impl;

import com.chixing.config.WebSocket;
import com.chixing.mapper.MyorderMapper;
import com.chixing.mapper.ShopMapper;
import com.chixing.pojo.BaseMessage;
import com.chixing.pojo.Myorder;
import com.chixing.pojo.MyorderExample;
import com.chixing.pojo.Shop;
import com.chixing.service.OrderService;
import com.chixing.util.DateUtil;
import com.chixing.util.MongodbUtils;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MyorderMapper myorderMapper;
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ServerResult save(Myorder myorder) {
        System.out.println(myorder);
        int rows = myorderMapper.insert(myorder);
        if (rows > 0) {
            System.out.println("添加订单成功");
            //下单后webSocket发送消息
//            webSocket.sendMessage ("订单通知");
            BaseMessage message = new BaseMessage ();
            Date orderDate = myorder.getMyorderDate ();
            message.setMessageDate (orderDate);
            System.out.println ("下单时间"+ orderDate);
            Integer shopId = myorder.getGetShopId ();
            Shop shop = shopMapper.selectByPrimaryKey (shopId);
            String shopName  = shop.getShopName();
            message.setShopName (shopName);
//            webSocket.sendMessage (shopName);
            String title = "订单通知";
            String messageText ="您的订单已下达";
            String status = "已查看";
            message.setMessageText (messageText);
            message.setStatus (status);
            message.setTitle (title);
            MongodbUtils.insert (message,"message");
            String createOrderDate = DateUtil.date2String (orderDate,"yyyy-MM-dd HH:mm:ss");
            String Msg ="订单通知,"+"您的订单已下达,"+shopName+","+createOrderDate+",已查看";
            webSocket.sendMessage (Msg);
//            webSocket.sendMessage (createOrderDate);
            return ServerResult.saveSussess(myorder);
        }
        return ServerResult.saveFail(null);
    }

    @Override
    public Myorder getById(Integer orderId) {
        Myorder myorder = myorderMapper.selectByPrimaryKey(orderId);
        return myorder;
    }

    @Override
    public List<Myorder> getOrderdetail(Integer cusId) {
        MyorderExample example = new MyorderExample ();
        example.createCriteria ().andMemberIdEqualTo (cusId);
        example.setOrderByClause("myorder_date desc");
        List<Myorder> myorderList = myorderMapper.selectByExample (example);
        System.out.println ("用户的订单列表"+myorderList);
        return myorderList;
    }

    @Override
    public Myorder getByCouponOrder(Integer memberId, Integer couponNo) {

        MyorderExample example = new MyorderExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andCouponNoEqualTo(couponNo);
        List<Myorder> myorderList = myorderMapper.selectByExample(example);

        if (myorderList.size() > 0 && myorderList != null)
            return myorderList.get(0);
        return null;
    }

    @Override
    public Integer updateOrderStatus(Myorder myorder) {
         Integer rows = myorderMapper.updateByPrimaryKey (myorder);
        System.out.println ("修改订单状态");
        return rows;
    }
}
